﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using System.Windows.Data;

namespace V2TEK.NET.SH.Silverlight.GanttView
{
    public class ExtendAttributeTextColumn : DataGridTextColumn
    {
        //public string ExtendAttributeKey { get; set; }

        //protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        //{
        //    if (dataItem is TaskEntryLayoutItem)
        //    {
        //        TaskEntryLayoutItem p = dataItem as TaskEntryLayoutItem;
        //        TextBlock txt = new TextBlock();
        //        txt.VerticalAlignment = VerticalAlignment.Center;
        //        if (p.Data.ExtendAttributes.ContainsKey(ExtendAttributeKey))
        //        {
        //            object o = p.Data.ExtendAttributes[ExtendAttributeKey];
        //            if (o != null)
        //                txt.Text = o.ToString();
        //        }
        //        return txt;
        //    }
        //    return base.GenerateElement(cell, dataItem);
        //}

        public ExtendAttributeTextColumn()
        {
            base.IsReadOnly = true;
        }

        public new bool IsReadOnly
        {
            get;
            set;
        }

        public string ExtendAttributeKey
        {
            get { return (string)GetValue(ExtendAttributeKeyProperty); }
            set { SetValue(ExtendAttributeKeyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ExtendAttributeKey.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ExtendAttributeKeyProperty =
            DependencyProperty.Register("ExtendAttributeKey", typeof(string), typeof(ExtendAttributeTextColumn), new PropertyMetadata(new PropertyChangedCallback(ExtendAttributeKeyChanged)));

        protected static void ExtendAttributeKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ExtendAttributeTextColumn)
            {
                ExtendAttributeTextColumn col = d as ExtendAttributeTextColumn;
                if (col.Binding == null)
                    col.Binding = new Binding("");
                else
                    col.Binding.Path = new PropertyPath("");
                //if (col.Binding.Converter == null)
                if (!(col.Binding.Converter is ExtendAttributeValueConverter))
                {
                    col.Binding.Converter = new ExtendAttributeValueConverter();
                    col.Binding.Mode = BindingMode.OneWay;
                }
                col.Binding.ConverterParameter = col.ExtendAttributeKey;
            }
        }
    }

    public class ExtendAttributeValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is TaskEntryLayoutItem)
            {

                var dic = ((TaskEntryLayoutItem)value).Data.ExtendAttributes;
                string key = parameter as string;
                return dic[key];
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
